package com.sskj.fabi.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.component.DaggerUserDataComponent;
import com.sskj.fabi.presenter.FabiFragmentPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.ViewPagerBoolean;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.FABI_FRAGMENT)
public class FabiFragment extends BaseFragment<FabiFragmentPresenter> {
    @BindView(R2.id.viewPager)
    ViewPagerBoolean viewPager;
    @BindView(R2.id.segmentTabLayout)
    SegmentTabLayout slidingTabLayout;
    @BindView(R2.id.ivPublish)
    ImageView tvPublish;
    @BindView(R2.id.ivRecord)
    ImageView ivRecord;
    private String[] titles = {App.INSTANCE.getString(R.string.fabipublishActivity7), App.INSTANCE.getString(R.string.fabipublishActivity8)};
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private int lastValue = -1;
    private boolean isLeft;
    private boolean isBuy = true; // 购买大厅

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_main;
    }

    @Override
    public FabiFragmentPresenter getPresenter() {
        return new FabiFragmentPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_BUY_SELL).withBoolean(Constans.IS_BUY, true).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_FRAGMENT_BUY_SELL).withBoolean(Constans.IS_BUY, false).navigation());
//        fragments.add((Fragment) ARouter.getInstance().build(RConfig.FABI_ORDER_RECORD_FRAGMENT).navigation());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragments));
        viewPager.setCanScrollble(false);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                slidingTabLayout.setCurrentTab(position);
                switch (position) {
                    case 0:
                        isBuy = true;
                        tvPublish.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        isBuy = false;
                        tvPublish.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        tvPublish.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setListener(() -> {
            if (viewPager.getCurrentItem() == 1) {
                if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {// 未登录
                    return false;
                }
//                if (!AppCircleCheckUtil.checkAuth(getActivity(), userData)) {
//                    return false;
//                }
            }
            return true;
        });
        slidingTabLayout.setTabData(titles);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

//        slidingTabLayout.setViewPager(viewPager, titles);
//        slidingTabLayout.setOnTabSelectListener(new HeySlidingTabLayout.OnTabSelectListener() {
//            @Override
//            public boolean onTabSelect(int position) {
//                switch (position) {
//                    case 0:
//                        isBuy = true;
//                        tvPublish.setVisibility(View.VISIBLE);
//                        break;
//                    case 1:
//                        isBuy = false;
//                        tvPublish.setVisibility(View.VISIBLE);
//                        break;
//                    case 2:
//                        tvPublish.setVisibility(View.GONE);
//                        break;
//                }
//                if (position == 2) {
//                    if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {// 未登录
//                        return false;
//                    }
////                    if (!AppCircleCheckUtil.checkAuth(getActivity(), userData)) {// 高级认证没有通过
////                        return false;
////                    }
//                }
//                return true;
//            }
//
//            @Override
//            public void onTabReselect(int position) {
//
//            }
//        });

        ClickUtil.click(tvPublish, () -> {
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }

            if (!AppCircleCheckUtil.checkShop(getActivity(), userData)) {  //是否是商家
                return;
            }

            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {  // 资金密码
                return;
            }
            ARouter.getInstance().build(RConfig.FABI_PUBLISH)
                    .withBoolean(Constans.IS_BUY, isBuy)
                    .navigation();
        });
        ClickUtil.click(ivRecord, () -> { // 交易记录
            if (!AppCircleCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.FABI_ORDER_RECORD)
                    .navigation();

        });
    }
}
